<?php
namespace Home\Biz;

class StringBiz{
	/**
	 * 获取promoter_order订单分成状态文本
	 */
	public static function getPromoterOrderRevenueStatusText($source){
		switch($source){
			case 0:
				return '待分成';
				break;
			case 1:
				return '无效';
				break;
			case 2:
				return '未分成';
				break;
			case 3:
				return '已分成';
				break;
			default:
				return strval($source);
				break;
		}
	}

	
	public static function getWithdrawStatusText($source){
		switch($source){
			case 1:
				return '提现申请中';
				break;
			case 2:
				return '提现成功';
				break;
			case 3:
				return '提现失败';
				break;
			default:
				return strval($source);
				break;
		}
	}
	
	/**
	 * 获取promoter_order订单状态文本
	 */
	public static function getPromoterOrderStatusText($source){
	switch($source){
			case 10:
				return '已提交';
				break;
			case 11:
				return '已审核';
				break;
			case 12:
				return '订单处理中';
				break;	
			case 21:
				return '待支付';
				break;
			case 22:
				return '已支付';
				break;
			case 25:
				return '物流处理中';
				break;
			case 31:
				return '已发货';
				break;
			case 32:
				return '已签收';
				break;
			case 35:
				return '已完成';
				break;
			case 41:
				return '退货中';
				break;
			case 42:
				return '退货完成';
				break;
			case 43:
				return '换货中';
				break;
			case 44:
				return '换货完成';
				break;
			case 51:
				return '用户取消';
				break;
			case 52:
				return '客服取消';
				break;
			case 53:
				return '系统自动取消';
				break;
			default:
				return strval($source);
				break;
		}
	}
	
	/**
	 * 判断字符串是否合法字符串
	 * @param unknown $str
	 */
	public static function isAddress($str){
		$str_len = mb_strlen($str, 'gb2312');
		if($str_len < 5 || $str_len > 50){
			return false;
		}
		
		if(!StringBiz::isExistChinese($str)){
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * 判断字符串是否存在中文
	 * @param unknown $str
	 */
	public static function isExistChinese($str){
		$x = mb_strlen($str,'gb2312');
		$y = strlen($str);
		
		if($x == $y){
			return false;
		}
		else{
			return true;
		}
		
	}
	
	public static function isZipcode($str){
		if (strlen($str) != 6)
		{
			return false;
		}
		else{
			if(is_numeric($str)){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	
	public static function getPromoterAmountAmountKindText($source){
		switch($source){
			case 1:
				return '增加';
				break;
			case 2:
				return '减少';
				break;
			default:
				return strval($source);
				break;
		}
	}
	
	public static function getPromoterAmountAmountTypeText($source){
		switch($source){
			case 11:
				return '客服增加';
				break;
			case 12:
				return '订单提成';
				break;
			case 13:
				return '充值';
				break;
			case 21:
				return '客服减少';
				break;
			case 22:
				return '提现';
				break;
			default:
				return strval($source);
				break;
		}
	}
	
	
	/**
	 * 判断字符串是否手机号码
	 * @param unknown $str
	 */
	public static function isCellphone($str){
		if(preg_match("/^((1[3,5,7,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/", $str)){
			//验证通过
			return true;
		}else{
			//手机号码格式不对
			return false;
		}
	}
	
	/**
	 * 判断字符串是否固话号码
	 * @param unknown $str
	 */
	public static function isPhone($str){
		if(preg_match("/^([0-9]{3,4}-)?[0-9]{7,8}$/", $str)){
			//验证通过
			return true;
		}else{
			//手机号码格式不对
			return false;
		}
	}
	
	public static function getRandomStr($count, $lst_char){
		$random_str = '';
	
		$lst_char_count = strlen($lst_char);
		for($i = 0; $i < $count; $i++){
			$seed = rand(0, $lst_char_count - 1);
			$random_str = $random_str.$lst_char[$seed];
		}
		 
		return $random_str;
	}
	
	// 身份证校验 start
	/**
	 * 是否身份证号码
	 */
	public static function isIdentity($id_card){
		if(strlen($id_card) == 18)
		{
			return StringBiz::idcard_checksum18($id_card);
		}
		elseif((strlen($id_card) == 15))
		{
			$id_card = StringBiz::idcard_15to18($id_card);
			return StringBiz::idcard_checksum18($id_card);
		}
		else
		{
			return false;
		}
	}
	
	public static function idcard_verify_number($idcard_base)
	{
		if(strlen($idcard_base) != 17)
		{
			return false;
		}
		//加权因子
		$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
		//校验码对应值
		$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
		$checksum = 0;
		for ($i = 0; $i < strlen($idcard_base); $i++)
		{
			$checksum += substr($idcard_base, $i, 1) * $factor[$i];
		}
		$mod = $checksum % 11;
		$verify_number = $verify_number_list[$mod];
		return $verify_number;
	}
	
	// 将15位身份证升级到18位
	public  static function idcard_15to18($idcard){
		if (strlen($idcard) != 15){
			return false;
		}else{
			// 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
			if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false){
				$idcard = substr($idcard, 0, 6) . '18'. substr($idcard, 6, 9);
			}else{
				$idcard = substr($idcard, 0, 6) . '19'. substr($idcard, 6, 9);
			}
		}
		$idcard = $idcard.StringBiz::idcard_verify_number($idcard);
		return $idcard;
	}
	
	// 18位身份证校验码有效性检查
	public static function idcard_checksum18($idcard){
		if (strlen($idcard) != 18){ return false; }
		$idcard_base = substr($idcard, 0, 17);
		if (StringBiz::idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))){
			return false;
		}else{
			return true;
		}
	}
	// 身份证校验 end
}